/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.dishes.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.entity.enums.ChooseType;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyetech.springstage.core.date.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.DishesMakeCategory;
import com.qinyeit.serviceapp.repository.dishes.DishesMakeCategoryRepository;
import com.qinyeit.serviceapp.service.dishes.DishesMakeCategoryService;

import java.util.List;

/** 
 * ClassName: DishesMakeCategoryServiceImpl <br/> 
 * Function: 菜品做法分类 业务实现 <br/> 
 * date: 2018-11-23 13:40:37 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class DishesMakeCategoryServiceImpl extends BaseService<DishesMakeCategory, Long> implements DishesMakeCategoryService {


	@Autowired
	private DishesMakeCategoryRepository dishesMakeCategoryRepository;


	@Override
	public void updateData(JSONObject data) {
		if(data==null){
			return;
		}
		JSONArray detailData = data.getJSONArray("details");
		if(CollectionUtils.isEmpty(detailData)){
			return;
		}

		JSONObject item ;
		DishesMakeCategory category = null;
		List<DishesMakeCategory> categoryList = Lists.newArrayList();
		for (int i = 0; i < detailData.size(); i++) {
			item = detailData.getJSONObject(i);
			category = this.findById(item.getLong("id"));
			category.setUpdateTimestamp(DateUtils.current());
			category.setChooseType(ChooseType.valueOf(item.getString("chooseType")));
			categoryList.add(category);
		}
		dishesMakeCategoryRepository.saveAll(categoryList);

	}
}